package com.hp.ongoing;

import java.awt.BorderLayout;
import java.io.InputStream;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swingBean.actions.ApplicationAction;
import org.swingBean.descriptor.GenericFieldDescriptor;
import org.swingBean.descriptor.XMLDescriptorFactory;
import org.swingBean.gui.JActButton;
import org.swingBean.gui.JBeanPanel;

import com.hp.ongoing.app.view.form.contract.IReportViewForm;
import com.hp.ongoing.app.view.strategy.ReportViewStrategy;
import com.hp.ongoing.entity.AutenticacaoUsuario;
import com.hp.ongoing.persistence.factory.ConnectionFactory;
import com.hp.ongoing.persistence.repository.JdbcRepository;
import com.hp.ongoing.util.FileUtil;
import com.hp.ongoing.util.Constantes;
import com.hp.ongoing.view.MainForm;

public class Login {

	private static final String AUTENTICACAO_USUARIO_DESCRIPTOR_XML = "com/hp/ongoing/app/view/descriptor/autenticacaoUsuarioForm.xml";
	static AutenticacaoUsuario autenticacaoUsuario = new AutenticacaoUsuario();

	final static Logger logger = LoggerFactory.getLogger(Login.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		InputStream inputDescriptor = FileUtil
				.getFilePathAsStream(AUTENTICACAO_USUARIO_DESCRIPTOR_XML);
		GenericFieldDescriptor descriptor = XMLDescriptorFactory
				.getFieldDescriptor(AutenticacaoUsuario.class, inputDescriptor,
						"autenticacaoUsuarioForm");
		final JBeanPanel<AutenticacaoUsuario> panel = new JBeanPanel<AutenticacaoUsuario>(
				AutenticacaoUsuario.class, descriptor);
		

		

		final JFrame frame = new JFrame("Logon");
		
		
		JActButton botaoConectar = new JActButton("Conectar",
				new ApplicationAction() {
					IReportViewForm form = ReportViewStrategy.getStrategy()
							.get("autenticacaoUsuarioForm");

					public void execute() {
						panel.populateBean(autenticacaoUsuario);
						try {
							// form.showForm(autenticacaoUsuario);													

							System.out.println(ConnectionFactory.getInstance()
									.getConnection(autenticacaoUsuario));

							final JdbcRepository repository = JdbcRepository
									.getInstance(autenticacaoUsuario);

							SwingUtilities.invokeLater(new Runnable() {
								public void run() {

									new MainForm(autenticacaoUsuario, frame);
									
									frame.setVisible(false);
								}
							});

						} catch (Exception e) {
							e.printStackTrace();
							logger.info(e.fillInStackTrace().toString());

							JOptionPane.showMessageDialog(null,
									Constantes.ERRO_CONEXAO, Constantes.TITULO_ATENCAO,
									JOptionPane.ERROR_MESSAGE);

						}
					}
				});
		
		JPanel panelButton = new JPanel();
		panelButton.add(botaoConectar);
		
		frame.setSize(230, 160);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		panel.setBorder(BorderFactory.createTitledBorder(null,null));
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		frame.getContentPane().add(panelButton, BorderLayout.SOUTH);
		frame.setLocationRelativeTo(null);
		frame.setResizable(false);
		frame.setVisible(true);

	}

}
